package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/kth-smallest-element-in-a-bst/
 * @create 2025/9/15 21:06
 * @since JDK17
 */

public class Solution49 {
    int ans = -1, k;
    void dfs(TreeNode root, int last) {
        if (root == null) {
            return ;
        }
        dfs(root.left, root.val);
        k--;
        if (k == 0) {
            ans = root.val;
            return;
        }
        dfs(root.right, root.val);
    }
    public int kthSmallest(TreeNode root, int k) {
        this.k = k;
        dfs(root, root.val);
        return ans;
    }
}
